export default {
    namespaced: true,
    state: () => ({
        cateList: JSON.parse(sessionStorage.getItem('cateList') || '[]'), //购物车数据
        orderList:JSON.parse(sessionStorage.getItem('orderList') || '[]'), //提交订单数据
        collectionGoodsList:JSON.parse(sessionStorage.getItem('collectionGoodsList') || '[]')//商品收藏列表
        
    }),
    mutations: {
        // 将购物车中的数据持久化存储到本地
        saveToStorage(state) {
            sessionStorage.setItem('cateList', JSON.stringify(state.cateList))
        },
        // 添加商品
        addGoods(state, goods) {

            let styleKeyResult = state.cateList.find((item) => {
                return item.goods_styleKey === goods.goods_styleKey && item.goods_sizeKey === goods.goods_sizeKey
            })
            console.log(styleKeyResult ? true : false);
            // console.log(sizeKeyResult);
            if (!styleKeyResult) {
                state.cateList.push(goods)
            } else {
                styleKeyResult.goods_stock += goods.goods_stock
            }
            this.commit('m_cart/saveToStorage')
        },
        // 删除商品
        delGoods(state, goods) {
            let styleKeyResult = state.cateList.find((item) => {
                return item.goods_styleKey === goods.goods_styleKey && item.goods_sizeKey === goods.goods_sizeKey
            })
            if (styleKeyResult) {
                state.cateList = state.cateList.filter(item => item !== styleKeyResult)
            }
            this.commit('m_cart/saveToStorage')
        },
        // 删除选中商品
        delGoodsChecked(state) {
            state.cateList = state.cateList.filter(item => !item.goods_checked)
            this.commit('m_cart/saveToStorage')
        },
        // 更新商品数量
        updateGoodsPrice(state, goods) {

            let styleKeyResult = state.cateList.find((item) => {
                return item.goods_styleKey === goods.goods_styleKey && item.goods_sizeKey === goods.goods_sizeKey
            })
            if (styleKeyResult) {
                styleKeyResult.goods_stock = goods.goods_stock
                this.commit('m_cart/saveToStorage')
            }
        },
        // 更新购物车商品的勾选状态
        updateGoodschecked(state, goods) {
            let styleKeyResult = state.cateList.find((item) => {
                return item.goods_styleKey === goods.goods_styleKey && item.goods_sizeKey === goods.goods_sizeKey
            })
            if (styleKeyResult) {
                styleKeyResult.goods_checked = goods.goods_checked
                this.commit('m_cart/saveToStorage')
            }
        },
        // 全选or反选
        updateAllGoodsState(state, newChecked) {
            state.cateList.forEach(item => item.goods_checked = newChecked)
            console.log(newChecked);
            this.commit('m_cart/saveToStorage')
        },
        //生成商品订单数据
        updateOrderList(state, obj) {
            if(obj.data!==undefined){
            
          
              var orderId = new Date().getTime()//订单编号
              obj.data=obj.data.map(v=>{
                var random=Math.floor(Math.random()*1000) 
                 return {...v,goods_type:obj.type,goods_orderId:orderId+random++}
            })
         
            state.orderList.unshift(obj.data[0])
            sessionStorage.setItem('orderList', JSON.stringify(state.orderList))
             return
            }
         
             let newArr = []
            newArr = state.cateList.filter(item => item.goods_checked)
            // 1.待付款 2.待发货 3.待收货 4.待评价
            var orderId = new Date().getTime()//订单编号
            newArr=newArr.map(v=>{
            var random=Math.floor(Math.random()*1000) 
           return {...v,goods_type:obj.type,goods_orderId:orderId+random++}
            })
            state.orderList=[ ...newArr,...state.orderList]
            sessionStorage.setItem('orderList', JSON.stringify(state.orderList))
           
        },
        // 添加收藏商品
        updataCollectionGoods(state,goods){
            const findGoods=state.collectionGoodsList.find(item=>item.itemInfo.itemId===goods.itemInfo.itemId)
           if(!findGoods){
            state.collectionGoodsList.push(goods)
           }else{
            state.collectionGoodsList=state.collectionGoodsList.filter(item=>item.itemInfo.itemId!==findGoods.itemInfo.itemId)
           }
            sessionStorage.setItem('collectionGoodsList',JSON.stringify(state.collectionGoodsList))
        },
        
       
    },
    getters: {
        // 商品总价
        total(state) {

            return state.cateList.filter(item => item.goods_checked).reduce((pre, i) => pre += i.goods_price * i.goods_stock, 0)
        },
        // 选中的商品
        checkedCount(state) {
            return state.cateList.filter(item => item.goods_checked)
        },

    }
}